package com.arithmetic.version1;

import java.util.ArrayList;
import java.util.List;
public class Exercise {
    private List<BinaryOperation> operations;

    public Exercise() {
        operations = new ArrayList<>();
    }

    public boolean contains(BinaryOperation operation) {
        for (BinaryOperation op : operations) {
            if (op.equals(operation)) {
                return true;
            }
        }
        return false;
    }

    public void generateAdditionExercise(int operationCount) {
        operations.clear();
        while (operations.size() < operationCount) {
            BinaryOperation newOp = BinaryOperation.generateAdditionOperation();
            if (!contains(newOp)) {
                operations.add(newOp);
            }
        }
    }

    public void generateSubstractExercise(int operationCount) {
        operations.clear();
        while (operations.size() < operationCount) {
            BinaryOperation newOp = BinaryOperation.generateSubstractOperation();
            if (!contains(newOp)) {
                operations.add(newOp);
            }
        }
    }

    public void generateExercise(int operationCount) {
        operations.clear();
        while (operations.size() < operationCount) {
            BinaryOperation newOp = BinaryOperation.generateBinaryOperation();
            if (!contains(newOp)) {
                operations.add(newOp);
            }
        }
    }

    public void formateAndDisplay(int columnsPerRow) {
        for (int i = 0; i < operations.size(); i++) {
            System.out.print((i + 1) + ". " + operations.get(i));
            if ((i + 1) % columnsPerRow == 0) {
                System.out.println();
            }
        }
        if (operations.size() % columnsPerRow != 0) {
            System.out.println();
        }
    }

    public int size() {
        return operations.size();
    }

    public BinaryOperation getOperation(int index) {
        return operations.get(index);
    }
}
